//控制层
app.controller('orderController', function ($scope, $controller, orderService) {

    $controller('baseController', {$scope: $scope});//继承
    // 1、未付款，2、已付款，3、未发货，4、已发货，5、交易成功，6、交易关闭,7、待评价
    $scope.status = ['', '未付款', '交易关闭', '已付款', '已发货', '交易成功', '待评价', '已评价'];
    //1:PC，2：H5，3：Android，4：IOS，5：WeChat
    $scope.paymentType = ['', '在线支付', '货到付款'];
    $scope.invoiceType = ['普通发票', '电子发票', '增值税发票'];
    //读取列表数据绑定到表单中
    $scope.findAll = function () {
        orderService.findAll().success(
            function (response) {
                $scope.list = response;
            }
        );
    };

    //分页
    $scope.findPage = function (page, rows) {
        orderService.findPage(page, rows).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    };

    //查询实体
    $scope.findOne = function (id) {
        orderService.findOne(id).success(
            function (response) {
                $scope.entity = response;
            }
        );
    };

    //保存
    $scope.save = function () {
        var serviceObject;//服务层对象
        console.log('order id :' + $scope.entity.orderId);
        if ($scope.entity.orderId != null) {//如果有ID
            serviceObject = orderService.update($scope.entity); //修改
        } else {
            serviceObject = orderService.add($scope.entity);//增加
        }
        serviceObject.success(
            function (response) {
                console.log(response);
                if (response.success) {
                    //重新查询
                    alert(response.message, function () {
                        //重新加载
                        $scope.reloadList();
                    });
                } else {
                    alert(response.message);
                }
            }
        );
    };

    $scope.searchEntity = {};//定义搜索对象

    //搜索
    $scope.search = function (page, rows) {
        orderService.search(page, rows, $scope.searchEntity).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }

    //更改状态
    $scope.updateStatus = function (id, status) {
        orderService.updateStatus(id, status).success(
            function (response) {
                if (response.success) {
                    $scope.reloadList();//重新加载
                } else {
                    alert(response.message);
                }
            }
        );
    }
});	
